package cn.ncu.wechat.controller;

import cn.ncu.wechat.service.MiniappConfigDraftService;
import cn.ncu.wechat.service.MiniappConfigService;
import cn.ncu.wechat.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping("/api/miniapp")
public class MiniappConfigController {
    @Autowired
    private MiniappConfigService miniappConfigService;
    @Autowired
    private MiniappConfigDraftService miniappConfigDraftService;

    @GetMapping("/config")
    public Result<?> getMiniAppConfig() {
        return miniappConfigService.getMiniAppConfig();
    }

    @PostMapping("/config")
    public Result<?> saveMiniAppConfig(@RequestBody Map<String, Object> request) {
        System.out.println("saveConfig");
        return miniappConfigDraftService.saveMiniAppConfig(request);
    }

    @PostMapping("/publish")
    public Result<?> publishMiniAppConfig(@RequestBody(required = false) Map<String, String> request,@RequestHeader("Authorization") Optional<String> authorizationHeader) {
        return miniappConfigService.publishMiniAppConfig(request,authorizationHeader.get());
    }

    @GetMapping("/publish/history")
    public Result<?> MiniAppConfigPublishHistory() {
        return miniappConfigService.MIniAppConfigPublishHistory();
    }

    @PostMapping("/rollback")
    public Result<?> MiniAppConfigRollback(@RequestBody Map<String, String> request) {
        return miniappConfigService.MiniAppConfigRollback(request);
    }

    @GetMapping("/qrcode")
    public Result<?> getQrCode(@RequestParam String path){
        return miniappConfigService.getQrCode(path);
    }

    @GetMapping("/version")
    public Result<?>getVersionNow(){
        return miniappConfigService.getVersionNow();
    }
}
